const tree = {
    val: 'a',
    children: [
        {
            val: 'b',
            children: [
                {
                    val: 'd',
                    children: [],
                },
                {
                    val: 'e',
                    children: [],
                }
            ],
        },
        {
            val: 'c',
            children: [
                {
                    val: 'f',
                    children: [],
                },
                {
                    val: 'g',
                    children: [],
                }
            ],
        }
    ],
};

const arr = []
const dfs = (root) => {
	arr.push(root.val)
	console.log(root.val)
	root.children.forEach(dfs)
}

const dfsNoRec = (root) => {
	let stack = [root]
	while(stack.length){
		const top = stack.pop()
		console.log(top.val)
		for (let i = top.children.length-1; i>=0; i--) {
		  stack.push(top.children[i])
		}
	}
}

//
// const dfs = (root) => {
//     console.log(root.val);
//     root.children.forEach(dfs);
// };

dfs(tree);
console.log(arr)
dfsNoRec(tree);
